/**
 * Created by wangzi on 2017/3/29.
 */
$(function () {
         var data = ['wangzi', 'aaaa'];
        var csrftoken = $.cookie('csrftoken');
var myDate = new Date();
var timerange = $('#timerange option:selected').text();


        $('#systemselect').bind('change', 'select', function () {
            var system = $('#systemselect option:selected').text();

            if (system) {
                $.ajax({
                    type: 'POST',
                    url: '/geterror/',

                    data: {
                        'system': system,
                        csrfmiddlewaretoken: csrftoken,

                    },
                    success: function (data) {
                        console.log(data);

                        if (data) {
                            $('#errorselect').find('option').remove().end();

                            $(data).each(function (i) {
                                $('#errorselect').append('<option value="' + data[i].errortype + '">' + data[i].errortype + '</option>');
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert('出现错误：' + errorThrown);
                    }
                });
            } else {
                $('#errorselect').find('option').remove().end();
            }


        });


        $('#configtb').delegate('.addtr', 'click',function () {
            var row = $(this).parents('tr').children('td');
            var type = $(this).parent().find('input[name="type"]').val();

            var comment = $(this).parent().find('input[name="comment"]').val();

            var time = row.find('.timerange option:selected').text();
            var mark = row.find('.inputmark').val();

            $.ajax({
                type: 'post',
                url: '/addtr/',
                data: {
                    'type': type,
                    'comment': comment,
                    'time': time,
                    'mark': mark,
                    csrfmiddlewaretoken: csrftoken
                },
                success: function (data) {
                    // your code
                    if (data) {
                        alert('提交成功！');
                        $('.inputmark').val('');
                        $("#refreshtable").load(location.href + " #refreshtable");

                    }
                    else {
                        alert('请检查错误！');
                    }
                }
            });
            return false;

        });


        $('#refreshtable').delegate('.delete', 'click', function () {
            var row = $(this).parents('tr').children('td');
            data = row.eq(0).text();

                     var r=confirm("是否删除本条记录");
                        if(r==true){
            $.ajax({
                async: false,

                type: 'post',
                url: '/deletetr/',
                data: {
                    'id': data, csrfmiddlewaretoken: csrftoken
                },
                success: function (data) {
                    // your code
                    if (data) {
                                      $("#refreshtable").load(location.href + " #refreshtable");

                    }
                    else {
                        alert('请点慢点！！！！');
                    }
                }
            });}

        });



        function MergeCell(tableId, startRow, endRow, col) {
        var tb = document.getElementById(tableId);
        if (col >= tb.rows[0].cells.length) {
            return;
        }
        //当检查第0列时检查所有行
        if (col == 0) {
            endRow = tb.rows.length - 1;
        }
        for (var i = startRow; i < endRow; i++) {
            //subCol:已经合并了多少列
            var subCol = tb.rows[0].cells.length - tb.rows[startRow].cells.length;
            //程序是自左向右合并，所以下一行一直取第0列
            if (tb.rows[startRow].cells[col - subCol].innerHTML == tb.rows[i + 1].cells[0].innerHTML) {
                //如果相同则删除下一行的第0列单元格
                tb.rows[i + 1].removeChild(tb.rows[i + 1].cells[0]);
                //更新rowSpan属性
                tb.rows[startRow].cells[col - subCol].rowSpan = (tb.rows[startRow].cells[col - subCol].rowSpan | 0) + 1;
                //当循环到终止行前一行并且起始行和终止行不相同时递归(因为上面的代码已经检查了i+1行，所以此处只到endRow-1)
                if (i == endRow - 1 && startRow != endRow) {
                    MergeCell(tableId, startRow, endRow, col + 1);
                }
            } else {
                //起始行，终止行不变，检查下一列
                MergeCell(tableId, startRow, i, col + 1);
                //增加起始行
                startRow = i + 1;
            }
        }
    }
    MergeCell('configtb', 0, 0, 0);

});